// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import GameEvent from "../../Commons/GameEvent";
import SaveManager from "../../Managers/SaveManager";
import UIManager from "../../Managers/UIManager";
import PlatformManager from "../../Platforms/PlatformManager";
import BaseUI from "../BaseUI";
import ChatItem from "../UIItems/ChatItem";

const { ccclass, property } = cc._decorator;

@ccclass
export default class ChatUI extends BaseUI {

    @property(cc.Prefab)
    chatItemPrefab: cc.Prefab = null;
    @property(cc.EditBox)
    chatEditBox: cc.EditBox = null;
    @property(cc.ScrollView)
    scrollview: cc.ScrollView = null;


    chatItemArr: ChatItem[] = [];

    protected onLoad(): void {
        for (let i = 0; i < 30; i++) {
            var chatNode = cc.instantiate(this.chatItemPrefab);
            chatNode.active = false;
            this.chatItemArr.push(chatNode.getComponent(ChatItem));
            this.scrollview.content.addChild(chatNode);
        }
        this.chatEditBox.string = "";
    }

    protected onEnable(): void {
        SaveManager.single.getChatMessage();

        GameEvent.eventNode.on(GameEvent.UPDATACHATMESSAGE_EVENT, this.onUpdataChatMessage, this);
    }

    protected onDisable(): void {
        GameEvent.eventNode.off(GameEvent.UPDATACHATMESSAGE_EVENT, this.onUpdataChatMessage, this);
    }
    onUpdataChatMessage() {
        this.drawView();
    }

    drawView() {
        let chatMessageList: any[] = SaveManager.single.chatMessageList;
        for (let i = 0; i < this.chatItemArr.length; i++) {
            if (i < chatMessageList.length) {
                this.chatItemArr[i].node.active = true;
                this.chatItemArr[i].drawItem(chatMessageList[i]);
            } else {
                this.chatItemArr[i].node.active = false;
            }
        }
    }

    async sendChatMessageClick() {
        if (this.chatEditBox.string.length == 0) {
            UIManager.single.showTips("请输入聊天内容。");
            return;
        }

        let result = await PlatformManager.single.basePlatform.sdk.sensitiveWordFilter(this.chatEditBox.string);
        if (result.code == 200) {
            if (result.data.isSensitive) {
                UIManager.single.showTips("聊天内容含敏感词，请重新输入。");
                this.chatEditBox.string = "";
            } else {
                this.sendChatMessage(this.chatEditBox.string, {
                    nickName: SaveManager.single.userdata.playerName,
                    avatar: SaveManager.single.userdata.playerHeadId
                });
            }
        } else {
            //UIManager.single.showTips(result.message);
            UIManager.single.showTips("聊天内容含敏感词，请重新输入。");
            this.chatEditBox.string = "";
        }
    }

    async sendChatMessage(message: string, options: any = null) {
        var sdk = PlatformManager.single.basePlatform.sdk;
        var accountData = SaveManager.single.accountData;
        let result = await sdk.sendChatMessage(accountData.username, message, options);
        if (result.code == 200) {
            this.chatEditBox.string = "";
            SaveManager.single.getChatMessage();
        }
    }


}
